import { get_user_info } from './_get-user-info';

/**
 * 验证当前登录用户页面权限
 * @param productid 项目productid
 * @param page 当期页面地址，使用fd.data.page.route
 * @param cacha 是否使用缓存
 * @example check(url, spaceid, productid, page, cacha)
 */
export default async function check(productid: string, page: string, cacha: boolean) {

	const user_info = await get_user_info(productid, cacha);
	if (user_info) {
		const menus = user_info.menus;
		if (!menus) {
			return { result: false, auth_code: 0 };
		} else {
			if (page) {
				const check_res = menus.filter((item) => {
					return item.package_url === page;
				});
				if (check_res.length === 0) {
					return { result: false, auth_code: 0 };
				} else {
					const auth_code = check_res[0].auth_code;
					return { result: true, auth_code };
				}
			} else {
				return { result: false, auth_code: 0 };
			}
		}
	} else {
		throw Error('Could not get user auth info');
	}
}
